import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import useLoginStore from '@/store/user/index.js'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/login.vue')
    },
    {
      path: '/main',
      name: 'main',
      redirect: '/main/home',
      component: () => import('@/views/main.vue'),
      children: [
        {
          path: '/main/home',
          name: 'home',
          component: () => import('@/views/home/home.vue')
        },
        {
          path: '/main/search',
          name: 'search',
          component: () => import('@/views/search/search.vue')
        },
        {
          path: '/main/search/detail',
          name: 'detail',
          component: () => import('@/views/taskDetail/detail.vue')
        },
        {
          // path: '/main/search/detail/checkRes/:rowData',
          path: '/main/search/detail/checkRes',
          name: 'checkRes',
          component: () => import('@/views/taskDetail/cpns/checkRes.vue')
        },
        {
          path: '/main/goods',
          name: 'goods',
          component: () => import('@/views/goods/goods.vue')
        },

        {
          path: '/main/system/user',
          name: 'user',
          component: () => import('@/views/system/user/user.vue')
        },

        {
          path: '/main/exterior',
          name: 'exterior',
          component: () => import('@/views/exterior/exterior.vue')
        },
        {
          path: '/main/dirExport',
          name: 'dirExport',
          component: () => import('@/views/dirExport/dirExport.vue')
        },
        {
          path: '/main/accout',
          name: 'accout',
          component: () => import('@/views/accout/accout.vue')
        },
        {
          path: '/main/material',
          name: 'material',
          component: () => import('@/views/material/material.vue')
        },
        {
          path: '/main/fixeNetwork',
          name: 'fixeNetwork',
          component: () => import('@/views/fixeNetwork/fixeNetwork.vue')
        },
        {
          path: '/main/InventoryAge',
          name: 'InventoryAge',
          component: () => import('@/views/InventoryAge/InventoryAge.vue')
        },
        {
          path: '/main/curingId',
          name: 'curingId',
          component: () => import('@/views/curingId/curingId.vue')
        },
      ]
    },

    // {
    //   path: '/exterior',
    //   name: 'exterior',
    //   redirect: '/exterior/exterior',
    //   component: () => import('@/views/main.vue'),
    //   children: [
    //     {
    //       path: '/exterior/exterior',
    //       name: 'exterior',
    //       component: () => import('@/views/exterior/exterior.vue')
    //     },
    //   ]

    // },
    {
      path: '/:pathMatch(.*)',
      component: () => import('@/not-found/not-found.vue')
    }
  ]
})

//路由鉴权
//进度条的加载小圆球不要
NProgress.configure({ showSpinner: false })

//定义不需要登录的路由
const whiteList = ['/login']

// router.beforeEach((to, from, next) => {
//   NProgress.start()
//   const loginStore = useLoginStore()
//   if (whiteList.includes(to.path)) {
//     //放行
//     next()
//   } else {
//     //需要登录，判断是否登录
//     if (loginStore.token) {
//       // console.log('loginStore.token',loginStore.token);
//       //方向
//       next()
//     } else {
//       //重定向到首页
//       next({ path: '/login' })
//     }
//   }
// })
router.beforeEach((to, from, next) => {
  NProgress.start()
  let userInfo = localStorage.getItem('loginInfo')

  if (whiteList.includes(to.path)) {
    if (to.path === '/login' && userInfo) {
      return next({ path: "/main/home" }); // 如果用户已登录并试图访问登录页，则重定向有权限访问的第一页
    }
    next()
  } else {
    //需要登录，判断是否登录
    // if (loginStore.token) {
    // console.log('loginStore.token',loginStore.token);
    //方向
    if (!userInfo) {
      return next({ path: '/login' })
    }
    next()
    // } else {
    //   //重定向到首页
    //   next({ path: '/login' })
    // }
  }
})


router.afterEach((to, from, next) => {
  NProgress.done()
})
export default router
